<template>
  <section>
    <!-- 工具栏 -->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="condition">
        <el-form-item>
          <el-input v-model="condition.jobNumber" placeholder="工号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="condition.name" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="condition.department" placeholder="部门"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="condition.roomNumber" placeholder="房间号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!-- 操作栏位 -->
    <el-col>
      <el-form :inline="true">
        <!-- <el-form-item>
          <el-button type="success" @click="newFormVisible = true" size="small">新增</el-button>
        </el-form-item>-->
        <el-form-item>
          <el-button type="success" @click="outExcel" size="small">导出EXCEL</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="updateInfo" size="small">更新工号部门</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!-- 内容展示 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      @selection-change="selsChange"
      v-loading="loading"
    >
      <el-table-column align="center" type="selection" fixed width="50"></el-table-column>
      <el-table-column align="center" label="序号" fixed type="index" width="50"></el-table-column>
      <el-table-column align="center" label="区域名称" prop="affiliationName" fixed min-width="150"></el-table-column>
      <!-- <el-table-column align="center" label="区域编号" prop="affiliationNumber" fixed min-width="100"></el-table-column> -->
      <!-- <el-table-column align="center" label="宿舍楼编号" prop="dormNumber" min-width="150"></el-table-column> -->
      <el-table-column fixed align="center" label="宿舍楼名称" prop="dormName" min-width="100"></el-table-column>
      <el-table-column fixed align="center" label="楼栋号" prop="number" min-width="80"></el-table-column>
      <el-table-column align="center" label="楼层" prop="layers" min-width="100"></el-table-column>
      <el-table-column align="center" label="房间号" prop="roomNumber" min-width="150"></el-table-column>
      <el-table-column align="center" label="房间类型" prop="roomType" min-width="80">
        <template slot-scope="scope">
          <!-- <div>{{scope.row.roomType}}</div> -->
          <div v-if="scope.row.roomType === '0'">单人间</div>
          <div v-else-if="scope.row.roomType === '1'">多人间</div>
          <div v-else-if="scope.row.roomType === '2'">公共房间</div>
          <div v-else></div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="性别" prop="roomSex" min-width="60"></el-table-column>
      <el-table-column align="center" label="床位号" prop="berthNumber" min-width="80"></el-table-column>
      <el-table-column align="center" label="床位状态" prop="berthType" min-width="100">
        <template slot-scope="scope">
          <!-- <div>{{scope.row.roomType}}</div> -->
          <div v-if="scope.row.berthType === '0'">禁用</div>
          <div v-else-if="scope.row.berthType === '1'">启用</div>
          <div v-else></div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="姓名" prop="name" min-width="100"></el-table-column>
      <el-table-column align="center" label="工号" prop="jobNumber" min-width="100"></el-table-column>
      <el-table-column align="center" label="部门" prop="department" min-width="100"></el-table-column>
      <el-table-column align="center" label="电话" prop="tel" min-width="150"></el-table-column>
      <!-- <el-table-column align="center" label="备注" prop="remark" min-width="100"></el-table-column> -->
      <el-table-column fixed="right" align="center" label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button size="small" @click="edit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="small" type="success" @click="changeBerth(scope.$index, scope.row)">更换床位</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-col :span="24" class="toolbar">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :total="totalPage"
        style="float:right;"
      ></el-pagination>
    </el-col>

    <!-- 编辑 -->
    <el-dialog title="编辑" :visible.sync="alterFormVisible" :close-on-click-modal="false">
      <el-form label-width="6rem" ref="alterBerth" :model="alterBerth">
        <el-form-item label="姓名">
          <el-input v-model="alterBerth.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="房间号">
          <el-input v-model="alterBerth.roomNumber" disabled></el-input>
        </el-form-item>
        <el-form-item label="床位">
          <el-input v-model="alterBerth.berthNumber" disabled></el-input>
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="alterBerth.tel" :disabled="alterTelControl"></el-input>
        </el-form-item>
        <el-form-item label="入住时间">
          <el-input v-model="alterBerth.inTime" disabled></el-input>
        </el-form-item>
        <el-form-item label="默认床位状态">
          <el-radio-group v-model="alterBerth.berthType" :disabled="berthTypeControl">
            <el-radio label="1">启用</el-radio>
            <el-radio label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="领用物品">
          <el-input v-model="alterDorm.goods"></el-input>
        </el-form-item>-->
        <el-form-item label="备注">
          <el-input v-model="alterBerth.remark" type="textarea" :rows="4" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="alterFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="alterSubmit" :loading="alterLoading">提交</el-button>
      </div>
    </el-dialog>

    <!-- 更换床位页面 -->
    <el-dialog title="更换床位" :visible.sync="changeBerthVisible" :close-on-click-modal="false">
      <el-form label-width="6rem" ref="changeBerth" :model="changeBerthForm">
        <el-form-item label="请选择床位">
          <el-select v-model="changeBerthForm.berthNumber">
            <el-option
              v-for="item in berth"
              :key="item.serial"
              :label="item.berthnumber"
              :value="item.serial"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="changeBerthVisible = false">取消</el-button>
        <el-button type="primary" @click.native="changeBerthSubmit" :loading="changeBerthLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import {
  searchBerth,
  alterBerth,
  getBerth,
  replaceBerth,
  updatePersonInfo
} from "../../api/berth.js";
export default {
  data() {
    return {
      condition: {
        jobNumber: null,
        name: null,
        department: null,
        roomNumber: null,
        page: 1
      },
      tableData: [
        {
          serial: null,
          affiliationName: null,
          affiliationNumber: null,
          dormNumber: null,
          dormName: null,
          number: null,
          layers: null,
          roomNumber: null,
          roomType: null,
          roomSex: null,
          berthNumber: null,
          berthType: null,
          name: null,
          jobNumber: null,
          department: null,
          tel: null,
          inTime: null,
          goods: null,
          remark: null
        }
      ],
      alterBerth: {
        name: null,
        roomNumber: null,
        berthNumber: null,
        tel: null,
        inTime: null,
        goods: null,
        remark: null
      },
      loading: false,
      alterFormVisible: false,
      currentPage: 1,
      totalPage: 1,
      newLoading: false,
      newFormVisible: false,
      alterFormVisible: false,
      alterLoading: false,
      berthTypeControl: false,
      alterTelControl: false,
      changeBerthVisible: false,
      changeBerthForm: { berthNumber: null, serial: null },
      berth: null,
      changeBerthLoading: false
    };
  },
  methods: {
    getData() {
      this.loading = true;
      searchBerth(this.condition).then(res => {
        this.loading = false;
        if (res) {
          this.tableData = res.msg;
          this.totalPage = res.page;
        } else {
          this.tableData = [];
          this.totalPage = 0;
          this.currentPage = 0;
        }
      });
    },
    // 查询
    search() {
      this.condition.page = 1;
      this.getData();
    },
    // 分页
    handleCurrentChange(val) {
      this.condition.page = val === undefined ? this.currentPage : val;
      this.getData();
    },
    updateInfo() {
      this.loading = true;
      updatePersonInfo().then(res => {
        this.loading = false;
        if (res) {
          this.$alert(res.msg, "系统提示", {
            confirmButtonText: "确定",
            callback: action => {
              console.log("OK");
            }
          });
        } else {
          this.$message.error("更新失败!");
        }
      });
    },
    // 点击编辑
    edit(index, row) {
      this.alterFormVisible = true;
      this.alterBerth = Object.assign({}, row);
      if (row.name !== "") {
        this.berthTypeControl = true;
        this.alterTelControl = false;
      } else {
        this.berthTypeControl = false;
        this.alterTelControl = true;
      }
    },
    alterSubmit(formName) {
      console.log(this.alterBerth);
      this.alterLoading = true;
      alterBerth(this.alterBerth).then(res => {
        this.alterLoading = false;
        this.alterFormVisible = false;
        if (res) {
          this.$message({
            type: "success",
            message: res.msg
          });
        } else {
          this.$message.error(res.msg);
        }
        this.search();
      });
    },
    outExcel() {
      window.location.href =
        "http://192.168.10.225:80/dormitoryA/building/Building/berthInfoExcelOut";
    },
    selsChange: function(sels) {
      this.sels = sels;
    },
    changeBerth(index, row) {
      // 无人床位禁止调床
      this.changeBerthForm = {
        serial: null,
        berthNumber: null
      };
      if (row.name === "") {
        this.$message.error("无人入住无法调整床位!");
      } else {
        this.changeBerthVisible = true;
        // 获取该房间床位
        getBerth(row).then(res => {
          if (res) {
            this.berth = res.msg;
            this.changeBerthForm.serial = index;
          } else {
            this.berth = null;
            this.changeBerthForm.serial = index;
          }
        });
      }
    },
    changeBerthSubmit() {
      console.log(this.changeBerthForm.berthNumber);
      // 没有更换床位不提交
      if (
        this.tableData[this.changeBerthForm.serial].serial ===
        this.changeBerthForm.berthNumber
      ) {
        this.$message.warning("没有更换床位");
      } else {
        this.changeBerthLoading = true;
        replaceBerth(
          this.changeBerthForm.berthNumber,
          this.tableData[this.changeBerthForm.serial].serial
        ).then(res => {
          this.changeBerthLoading = false;
          if (res) {
            this.$message.success(res.msg);
          } else {
            this.$message.error(res.msg);
          }
          this.changeBerthVisible = false;
          this.search();
        });
      }
    }
  },
  mounted() {
    this.getData();
  }
};
</script>
<style>
</style>